package sanphone.com.runningschool.JuShou;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import sanphone.com.runningschool.BaseActivity;
import sanphone.com.runningschool.ForceOffline;
import sanphone.com.runningschool.InterfaceLocation;
import sanphone.com.runningschool.R;
import sanphone.com.runningschool.RequestHttp.RequestHttp;
import sanphone.com.runningschool.Saying.SayingDetailActivity;

public class JuShouDetailActivity extends BaseActivity implements View.OnClickListener {


    private String userId, behaviorId;
    private TextView nickName;
    private TextView thing_name;
    private TextView getTime;
    private TextView deadline;
    private TextView getLocation;
    private TextView sendLocation;
    private TextView weight;
    private TextView rewarding;
    private CheckBox confirm;
    private Button accept;
    private boolean flag = false;
    private Intent intent;
    private SharedPreferences sprf;
    private String userName;
    private RelativeLayout Bottom_linear;
    private View tab_divide;
    private TextView status;
    private ImageView delete;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sprf = getSharedPreferences("data", MODE_PRIVATE);
        setContentView(R.layout.activity_jushou_detail);
        sprf = getSharedPreferences("data", MODE_PRIVATE);
        ForceOffline offline = new ForceOffline(sprf.getString("schoolId", null), this);
        offline.check();
        initView();

        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        getData();
        setData();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                JuShouDetailActivity.this.finish();//返回上一层活动
                break;
            default:
                break;
        }
        return true;
    }


    private void initView() {
        nickName = findViewById(R.id.nickName);
        thing_name = findViewById(R.id.thing_name);
        getTime = findViewById(R.id.getTime);
        deadline = findViewById(R.id.deadline);
        getLocation = findViewById(R.id.getLocation);
        sendLocation = findViewById(R.id.sendLocation);
        weight = findViewById(R.id.weight);
        tab_divide = findViewById(R.id.tab_divide2);
        rewarding = findViewById(R.id.rewarding);
        confirm = findViewById(R.id.confirm);
        accept = findViewById(R.id.accept);
        status = findViewById(R.id.status);
        Bottom_linear = findViewById(R.id.Bottom_linear);
        delete=findViewById(R.id.j_delete);
    }

    private void getData() {
        SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
        userId = pref.getString("schoolId", null);
        behaviorId = getIntent().getStringExtra("behaviorId");
    }

    private void setData() {
        intent = getIntent();
        sprf = getSharedPreferences("data", MODE_PRIVATE);
        userName = sprf.getString("nickName", null);
        String name = intent.getStringExtra("nickName");
        if (userName.equals(name)) {
            tab_divide.setVisibility(View.GONE);
            Bottom_linear.setVisibility(View.GONE);
            status.setVisibility(View.VISIBLE);
            delete.setVisibility(View.VISIBLE);
            delete.setOnClickListener(this);
        } else {
            tab_divide.setVisibility(View.VISIBLE);
            Bottom_linear.setVisibility(View.VISIBLE);
            status.setVisibility(View.GONE);
            delete.setVisibility(View.GONE);
            accept.setOnClickListener(this);
        }
        nickName.setText(name);
        thing_name.setText(intent.getStringExtra("thingName") + "\n" + intent.getStringExtra("detail"));
        getLocation.setText(intent.getStringExtra("thingPlace"));
        getTime.setText(intent.getStringExtra("thingTime"));
        sendLocation.setText(intent.getStringExtra("deliverPlace"));
        deadline.setText(intent.getStringExtra("deliverTime"));
        rewarding.setText(String.valueOf(intent.getDoubleExtra("money", 0.0)) + " RMB");
        weight.setText(String.valueOf(intent.getDoubleExtra("thingWeight", 0.0)) + " kg");
        confirm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    flag = true;
                } else {
                    flag = false;
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.accept:
                if (flag == true) {
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.submit(new Runnable() {
                        @Override
                        public void run() {
                            JSONObject json = new JSONObject();
                            try {
                                json.put("schoolId", userId);
                                json.put("behaviorId", behaviorId);
                                RequestHttp requestHttp = new RequestHttp(InterfaceLocation.getLocation() + "acceptBehavior",JuShouDetailActivity.this);
                                String response = requestHttp.post(json);
                                JSONObject responseJson = new JSONObject(response);
                                if (responseJson.getString("data").equals("任务接受成功")) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            AlertDialog.Builder alter = new AlertDialog.Builder(JuShouDetailActivity.this);
                                            alter.setTitle("温馨提示").setMessage("任务接受成功,请到我的订单中查看任务的详细信息").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    JuShouDetailActivity.this.finish();
                                                }
                                            });
                                            alter.create();
                                            alter.show();
                                        }
                                    });
                                } else {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            AlertDialog.Builder alter = new AlertDialog.Builder(JuShouDetailActivity.this);
                                            alter.setTitle("温馨提示").setMessage("任务已被别人领取或者已关闭").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    JuShouDetailActivity.this.finish();
                                                }
                                            });
                                            alter.create();
                                            alter.show();
                                        }
                                    });
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    executorService.shutdown();
                } else {
                    AlertDialog.Builder alter = new AlertDialog.Builder(JuShouDetailActivity.this);
                    alter.setTitle("温馨提示").setMessage("请仔细阅读条款并同意").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    });
                    alter.create();
                    alter.show();
                }
                break;
            case R.id.j_delete:
                AlertDialog.Builder builder=new AlertDialog.Builder(JuShouDetailActivity.this);
                builder.setTitle("温馨提示");
                builder.setMessage("是否删除订单？");
                builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        deleteOrder();
                    }
                });
                builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.show();
                break;
        }
    }

    private void deleteOrder() {
        Log.e("userid behaviorId ",userId+" "+behaviorId );
        ExecutorService executorService=Executors.newSingleThreadExecutor();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject json=new JSONObject();
                    json.put("schoolId",userId);
                    json.put("behaviorId",Integer.valueOf(behaviorId));
                    RequestHttp requestHttp=new RequestHttp(InterfaceLocation.getLocation()+"userDeleteBehavior",JuShouDetailActivity.this);
                    String response=requestHttp.post(json);
                    JSONObject responseJson=new JSONObject(response);
                    String data=responseJson.getString("data");
                    Log.e("deleteOrder", data);
                    if(data.equals("删除成功")){
                        Toast.makeText(JuShouDetailActivity.this,"订单删除成功！",Toast.LENGTH_SHORT).show();
                        JuShouDetailActivity.this.finish();
                    }else{
                        Toast.makeText(JuShouDetailActivity.this,"订单删除失败,请重试！",Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        executorService.shutdown();
    }
}
